$(function(){

	// 图片轮播
	var Scroll=(function(){
		return function(){
			var starScroll = document.getElementById("starScroll"),
				ScrollOne = document.getElementById("ScrollOne"),
				aCollection = ScrollOne.getElementsByTagName("a"),
				aLength = aCollection.length,
				ScrollTwo = document.getElementById("ScrollTwo"),
				btn_left = document.getElementById("btn_left"),
				btn_right = document.getElementById("btn_right");
			var Width = 874,Current =1,Rate = 10,TimeTimeout=1500,TimeInterval=20,MarginRight=0;
			var SInterval=null,STimeout=null;
			var flag=true;
			function ScrollLeft(){
				var CountWidth = Current*Width+Current*MarginRight,
					SLeftPara=starScroll.scrollLeft;
				if(ScrollTwo.offsetWidth-SLeftPara==0){
					starScroll.scrollLeft=0;
					Current=0;
				}
				if(CountWidth-SLeftPara==0){
					Forward();
				}
				else{
					var v = Math.round((CountWidth-SLeftPara)/Rate);
					v = v<1 ? 1 : v;
					SetScrollLeft(SLeftPara+v);
				}
			}
			function SetScrollLeft(scrollleft){
				starScroll.scrollLeft=scrollleft;
			}
			function Init(){
				flag=false;
				SInterval=setInterval(ScrollLeft,TimeInterval);
			}
			function Forward(){
				clearInterval(SInterval);
				Current++;
				flag=true;
				STimeout = setTimeout(Init,TimeTimeout);
			}
			btn_right.onclick=function(){
				if(flag){
					clearTimeout(STimeout);
					Init();
				}
			}
			function ScrollRight(){
				var CountWidth = Current*Width+Current*MarginRight,
					SLeftPara = starScroll.scrollLeft;
				if(CountWidth-SLeftPara==0){
					Forward();
				}
				else{
					var v = Math.round((CountWidth-SLeftPara)/Rate);
					v = v>-1?-1:v;
					SetScrollLeft(SLeftPara+v);
				}
			}
			btn_left.onclick=function(){
				if(!flag){
					return;
				}
				flag=false;
				clearTimeout(STimeout);
				if(Current==1){
					SetScrollLeft(ScrollTwo.offsetWidth);
					Current=aLength+1;
				}
				Current-=2;
				SInterval = setInterval(ScrollRight,TimeInterval);
			};
			if(aLength>0){
				starScroll.scrollLeft=0;
				ScrollTwo.innerHTML = ScrollOne.innerHTML;
				STimeout = setTimeout(Init,TimeTimeout);
			}
		}
	})();
	Scroll();
});

